#include "StdAfx.h"
#include "Utility.h"
#include "CustomTabCtrl.h"
#include "../Resource.h"

#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;

IMPLEMENT_DYNAMIC(CCustomTabCtrl, CTabCtrl)

CCustomTabCtrl::CCustomTabCtrl(void)
{
	m_strText[0] = _T("Home");
	m_strText[1] = _T("Status");
	m_strText[2] = _T("Export");

	m_nFontSizeNormal = 9;
	m_pFontSizeHilight = 15;

	m_pFontFamily = NULL;
	m_strTextFormat = NULL;
	m_enFontUnit = UnitPoint;

	
	m_nFontStyleNormal = FontStyleRegular;
	m_nFontStyleHilight = FontStyleRegular;//FontStyleBold;
	
	m_pBrushNormal = m_pBrushHilight = NULL;
	m_pFontNormal = m_pFontHilight = NULL;


	m_bHovered = FALSE;
	m_bIsTracking = FALSE;

	CUtility::ImageFromIDResource(IDR_TAB0, _T("PNG"), m_pImg[0]);
	CUtility::ImageFromIDResource(IDR_TAB0, _T("PNG"), m_pImg[1]);
	CUtility::ImageFromIDResource(IDR_TAB0, _T("PNG"), m_pImg[2]);
	CUtility::ImageFromIDResource(IDR_TAB1, _T("PNG"), m_pImg[3]);
}

CCustomTabCtrl::~CCustomTabCtrl(void)
{
	if(m_pFontNormal!=NULL){ delete m_pFontNormal;  m_pFontNormal=NULL; }
	if(m_pFontHilight!=NULL){ delete m_pFontHilight;  m_pFontHilight=NULL; }
	if(m_pBrushNormal!=NULL){ delete m_pBrushNormal;  m_pBrushNormal=NULL; }
	if(m_pBrushHilight!=NULL){ delete m_pBrushHilight;  m_pBrushHilight=NULL; }
	if(m_pFontFamily!=NULL){ delete m_pFontFamily;  m_pFontFamily=NULL; }
	if(m_strTextFormat!=NULL){ delete m_strTextFormat;  m_strTextFormat=NULL; }	

	for(int i = 0; i < 4; ++i)
		if ( m_pImg[i] )
			delete m_pImg[i];
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//Common Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomTabCtrl::SetBgDC(HDC hBgDC, HDC hMemDC)
{
	m_hBgDC = hBgDC;
	m_hMemDC = hMemDC;
}

void CCustomTabCtrl::CreateFont()
{
	//----------------------new FontFamily
	if(m_pFontFamily == NULL)
		m_pFontFamily = new FontFamily(L"Arial");
	if(FontFamilyNotFound == m_pFontFamily->GetLastStatus())
	{
		if (this->m_hWnd)
		{
			CFont *pCurFont = this->GetFont();  LOGFONT LgFong;  pCurFont->GetLogFont(&LgFong);
			if(m_pFontFamily){ delete m_pFontFamily;  m_pFontFamily = NULL;  m_pFontFamily = new FontFamily(LgFong.lfFaceName); }
		}
	}
	
	//----------------------new StringFormat
	if(m_strTextFormat == NULL)
	{
		m_strTextFormat = new StringFormat;
		m_strTextFormat->SetAlignment(StringAlignmentCenter);
		m_strTextFormat->SetLineAlignment(StringAlignmentCenter);
		m_strTextFormat->SetTrimming(StringTrimmingEllipsisCharacter);
	}
	
	//----------------------new Font
	if(m_pFontNormal == NULL)
		m_pFontNormal = new Font(m_pFontFamily,(Gdiplus::REAL)m_nFontSizeNormal, m_nFontStyleNormal, m_enFontUnit);
	if(m_pFontHilight == NULL)
		m_pFontHilight = new Font(m_pFontFamily,(Gdiplus::REAL)m_pFontSizeHilight, m_nFontStyleHilight, m_enFontUnit);


	//----------------------new FontBrush
	if (m_pBrushNormal == NULL)
		m_pBrushNormal = new SolidBrush(Color(247, 25, 25, 25));	
	if (m_pBrushHilight == NULL)
		m_pBrushHilight = new SolidBrush(Color(247, 247, 247, 247));
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Drawing
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomTabCtrl::DrawUI()
{
	CDC* pDC = GetDC();
	int nTab = 0;
	nTab = GetItemCount();
	if(nTab != 0)
	{
		DRAWITEMSTRUCT dis;            dis.CtlType = ODT_TAB;     dis.CtlID = GetDlgCtrlID();
		dis.hwndItem = GetSafeHwnd();  dis.hDC = pDC->m_hDC;        dis.itemAction = ODA_DRAWENTIRE;

		while (nTab--)
		{
			dis.itemID = nTab;   dis.itemState = 0;
			VERIFY(GetItemRect(nTab, &dis.rcItem));
			DrawItem(&dis);
		}
	}

	ReleaseDC(pDC);
}


void CCustomTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	int nItem = lpDrawItemStruct->itemID;

	CRect prect;
	HWND hParentWnd = ::GetParent(m_hWnd);
	::GetWindowRect(hParentWnd, &prect);

	CRect rect(lpDrawItemStruct->rcItem);
	CRect rectCtrl;

	GetWindowRect(&rectCtrl);

	rect.left += rectCtrl.left;
	rect.right += rectCtrl.left;
	rect.top += rectCtrl.top - 7;
	rect.bottom += rectCtrl.top - 7;
	
	rect.OffsetRect( -prect.left, -prect.top);

	BitBlt(m_hMemDC, rect.left, rect.top - 2, rect.Width(), rect.Height() + 3, m_hBgDC, rect.left, rect.top, SRCCOPY);
	Graphics graphics( m_hMemDC );

	int nSel = GetCurSel();
	if(nSel == nItem)
	{
		graphics.DrawImage(m_pImg[3], rect.left, rect.top, rect.Width(), rect.Height());
		if(!m_strText[nItem].IsEmpty())
		{
			CreateFont();
			graphics.DrawString(m_strText[nItem].GetBuffer(), -1, m_pFontHilight, 
				                RectF((Gdiplus::REAL)rect.left, (Gdiplus::REAL)(rect.top + 5), 
								      (Gdiplus::REAL)rect.Width(), (Gdiplus::REAL)(rect.Height() - 5)),
				                m_strTextFormat, m_pBrushHilight);
			m_strText[nItem].ReleaseBuffer();
		}
	}
	else
	{
		if(m_bHovered)
		{
			CPoint ptCurPos; GetCursorPos(&ptCurPos);
			CRect rcWindow; GetWindowRect(rcWindow);
			ptCurPos.x -= rcWindow.left;
			ptCurPos.y -= rcWindow.top;

			if(ptCurPos.x > 0 && ptCurPos.y > 0 && PtInRect(rect, ptCurPos))
				graphics.DrawImage(m_pImg[2], rect.left, rect.top, rect.Width(), rect.Height());
			else
				graphics.DrawImage(m_pImg[1], rect.left, rect.top, rect.Width(), rect.Height());
		}
		else
		{
			graphics.DrawImage(m_pImg[1], rect.left, rect.top, rect.Width(), rect.Height());
		}

		if(!m_strText[nItem].IsEmpty())
		{
			CreateFont();
			graphics.DrawString(m_strText[nItem].GetBuffer(), -1, m_pFontNormal, 
				                RectF((Gdiplus::REAL)rect.left, (Gdiplus::REAL)(rect.top + 5),
								      (Gdiplus::REAL)rect.Width(), (Gdiplus::REAL)(rect.Height() - 5)),
							    m_strTextFormat, m_pBrushNormal);
			m_strText[nItem].ReleaseBuffer();
		}
	}

	CUtility::UpdateWindow(hParentWnd, m_hMemDC, prect);
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Message
/*********************************************************************************************************************/
/*********************************************************************************************************************/
BEGIN_MESSAGE_MAP(CCustomTabCtrl, CTabCtrl)
	ON_WM_DRAWITEM()
	ON_WM_MOUSEMOVE()
	ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
	ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
END_MESSAGE_MAP()

void CCustomTabCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	TRACKMOUSEEVENT tme;
	tme.cbSize = sizeof(tme);
	tme.hwndTrack = m_hWnd;
	tme.dwFlags = TME_LEAVE|TME_HOVER;
	tme.dwHoverTime = 1;
	_TrackMouseEvent(&tme);

	CTabCtrl::OnMouseMove(nFlags, point);
}

LRESULT CCustomTabCtrl::OnMouseLeave(WPARAM wparam, LPARAM lparam)
{
	m_bHovered = false;
	CDC* pDC = GetDC();
	int nTab = 0;
	nTab = GetItemCount();
	if(nTab != 0)
	{
		DRAWITEMSTRUCT dis;            dis.CtlType = ODT_TAB;     dis.CtlID = GetDlgCtrlID();
		dis.hwndItem = GetSafeHwnd();  dis.hDC = pDC->m_hDC;        dis.itemAction = ODA_DRAWENTIRE;

		while (nTab--)
		{
			dis.itemID = nTab;   dis.itemState = 0;
			VERIFY(GetItemRect(nTab, &dis.rcItem));
			DrawItem(&dis);
		}
	}

	ReleaseDC(pDC);
	return 0;
}

LRESULT CCustomTabCtrl::OnMouseHover(WPARAM wparam, LPARAM lparam) 
{
	m_bHovered = true;
	CDC* pDC = GetDC();
	int nTab = 0;
	nTab = GetItemCount();
	if(nTab != 0)
	{
		DRAWITEMSTRUCT dis;            dis.CtlType = ODT_TAB;     dis.CtlID = GetDlgCtrlID();
		dis.hwndItem = GetSafeHwnd();  dis.hDC = pDC->m_hDC;        dis.itemAction = ODA_DRAWENTIRE;

		while (nTab--)
		{
			dis.itemID = nTab;   dis.itemState = 0;
			VERIFY(GetItemRect(nTab, &dis.rcItem));
			DrawItem(&dis);
		}
	}

	ReleaseDC(pDC);
	return 0;
}

BOOL CCustomTabCtrl::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}

void CCustomTabCtrl::PreSubclassWindow()
{
	// TODO: Add your specialized code here and/or call the base class
//	ModifyStyle(0, BS_OWNERDRAW);
	CTabCtrl::PreSubclassWindow();
}

void CCustomTabCtrl::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CTabCtrl::OnPaint() for painting messages

	int nTab = 0;
	nTab = GetItemCount();
	if(nTab != 0)
	{
		DRAWITEMSTRUCT dis;            dis.CtlType = ODT_TAB;     dis.CtlID = GetDlgCtrlID();
		dis.hwndItem = GetSafeHwnd();  dis.hDC = dc.m_hDC;        dis.itemAction = ODA_DRAWENTIRE;

		while (nTab--)
		{
			dis.itemID = nTab;   dis.itemState = 0;
			VERIFY(GetItemRect(nTab, &dis.rcItem));
			DrawItem(&dis);
		}
	}
}
